package com.gsy.controller.student;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gsy.aop.annotation.ParameterValid;
import com.gsy.entity.OpinionsEntity;
import com.gsy.enumerate.SystemStatusEnum;
import com.gsy.request.AddOpinionsRequest;
import com.gsy.response.BaseResponse;
import com.gsy.service.OpinionsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gsy
 * @since 2022-03-08
 */
@RestController
@RequestMapping(value = "opinions",consumes = "application/json", produces = "application/json")
@Api(value = "controller", tags = {"tb_opinions 操作接口"})
public class OpinionsController {

    private final static Logger LOGGER = LoggerFactory.getLogger(OpinionsController.class);

    @Resource
    private OpinionsService opinionsService;

    /**
    * 新增
    * @param request
    * @return
    */
    @ApiOperation(value = "新增tb_opinions信息", notes = "根据请求参数信息新增tb_opinions")
    @PostMapping("opinions")
    @ParameterValid(apiName = "新增tb_opinions信息", formatObj = AddOpinionsRequest.class)
    public BaseResponse addOpinions(@RequestBody AddOpinionsRequest request) {

        OpinionsEntity entity = new OpinionsEntity();
        BeanUtils.copyProperties(request, entity);
        entity.setStatus((short) 0);

        Boolean flag = opinionsService.save(entity);
        if (flag) {
            return new BaseResponse(SystemStatusEnum.SUCCESS);
        } else {
            return new BaseResponse(SystemStatusEnum.FAIL);
        }
    }


    /**
    * 获取列表
    * @param teacherId
    * @return
    */
    @ApiOperation(value = "获取tb_opinions列表", notes = "根据请求参数搜索获取tb_opinions列表")
    @GetMapping("opinions/{teacherId}/{currPage}/{pageNum}")
    public BaseResponse getOpinionsList(@PathVariable Long teacherId, @PathVariable Integer pageNum, @PathVariable Integer currPage) {

        Page<OpinionsEntity> entities = opinionsService.page(new Page<>(currPage,pageNum),Wrappers.lambdaQuery(OpinionsEntity.class)
                .eq(OpinionsEntity::getTeacherId,teacherId));

        if (CollectionUtil.isNotEmpty(entities.getRecords())) {
            BaseResponse success = new BaseResponse(SystemStatusEnum.SUCCESS);
            success.setData(entities.getRecords());
            success.setTotal(entities.getTotal());
            return success;
        } else {
            return new BaseResponse(SystemStatusEnum.SUCCESS.getCode(), "获取数据为空！");
        }
     }

}
